/* author: Mariana Horita
   mariana.horita@gmail.com
   file: A3E5.cs*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Resolucao
{
    public partial class A3E5 : Form
    {
        public A3E5()
        {
            InitializeComponent();

            Cursor c1 = new Cursor(@"..\..\Cursor1.cur");
            Cursor c2 = new Cursor(@"..\..\Cursor2.cur");

            txtCompromisso.Cursor = c1;

            mcCalendario.Cursor = c2;
        }

        private void btInsert_Click(object sender, EventArgs e)
        {
            bool inserted = false;

            foreach (TreeNode tn in trvAgenda.Nodes)
            {
                if (tn.Text == mcCalendario.SelectionStart.ToShortDateString())
                {
                    if (tn.Nodes.Count == 4)
                    {
                        MessageBox.Show("Esta data já possui 4 compromissos!");
                        return;
                    }

                    tn.Nodes.Add(txtCompromisso.Text);
                    inserted = true;
                }
            }

            if (! inserted)
            {
                TreeNode tn = trvAgenda.Nodes.Add(mcCalendario.SelectionStart.ToShortDateString());

                tn.Nodes.Add(txtCompromisso.Text);
            }

            txtCompromisso.Clear();
        }

        private void trvAgenda_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent == null)
            {
                pgbPorcentagem.Value = e.Node.Nodes.Count;
            }
            else
            {
                pgbPorcentagem.Value = 0;
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (trvAgenda.SelectedNode != null)
            {
                trvAgenda.SelectedNode.Remove();
            }
        }

        private void btOpen_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName.Length > 0)
            {
                try
                {
                    StreamReader sr;

                    sr = new StreamReader(openFileDialog1.FileName);

                    int lastIndex = 0;

                    TreeNode lastNode = new TreeNode();

                    while (!sr.EndOfStream)
                    {
                        string str = sr.ReadLine();

                        if (str.Length > 0)
                        {
                            int currentIndex = Convert.ToInt32(str.Split(' ').First());

                            string content = str.Remove(0, currentIndex.ToString().Count() + 1);

                            if (lastIndex == currentIndex)
                            {
                                if (lastNode.Parent == null)
                                {
                                    lastNode = trvAgenda.Nodes.Add(content);
                                }
                                else
                                {
                                    lastNode = lastNode.Parent.Nodes.Add(content);
                                }
                            }
                            else if (lastIndex > currentIndex)
                            {
                                for (int i = currentIndex; i < lastIndex; i++)
                                    lastNode = lastNode.Parent;

                                if (lastNode == null || lastNode.Parent == null)
                                {
                                    lastNode = trvAgenda.Nodes.Add(content);
                                }
                                else
                                {
                                    lastNode = lastNode.Parent.Nodes.Add(content);
                                }

                            }
                            else
                            {

                                lastNode = lastNode.Nodes.Add(content);
                            }

                            lastIndex = currentIndex;

                        }
                    }

                    sr.Close();

                }
                catch (Exception ex)
                {

                    MessageBox.Show("File can not be opened");
                }
            }
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName.Length > 0)
            {
                StreamWriter wr;

                wr = new StreamWriter(saveFileDialog1.FileName, false);

                foreach (TreeNode tn in trvAgenda.Nodes)
                {
                    wr.WriteLine(0 + " " + tn.Text, true);

                    WriteNode(tn, 1, ref wr);
                }

                wr.Close();
            }
        }

        private void WriteNode(TreeNode tn, int index, ref StreamWriter wr)
        {
            foreach (TreeNode t in tn.Nodes)
            {
                wr.WriteLine(index + " " + t.Text, true);

                if (t.Nodes.Count > 0)
                {
                    WriteNode(t, index + 1, ref wr);
                }

            }
        }
    }
}
